<?php
namespace app\client\wxpay;
use app\client\lib\PayHandle;

include_once __DIR__ . '/wxpay/WxPay.Api.php';
include_once __DIR__ . '/wxpay/WxPay.Notify.php';
/**
 * 异步通知
 * **/

class Notify extends \WxPayNotify {
    
    public $payResult = false; //此属性是自定义的，用来判断是否支付成功
    public $msg = 'OK';
    public $orderNo = '';
    public $tradeNo = '';
    public $callbackData = null;
    
    public $action = '';
    
    //查询订单
    public function Queryorder($transaction_id) {
        $input = new \WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        
        $config = new Config();
        $result = \WxPayApi::orderQuery($config, $input);
        if(array_key_exists('return_code', $result)
            && array_key_exists('result_code', $result)
            && $result['return_code'] == 'SUCCESS'
            && $result['result_code'] == 'SUCCESS')
        {
            return true;
        }
        
        return false;
    }
    
    public function NotifyProcess($objData, $config, &$msg) {
        $data = $objData->GetValues();
        //TODO 1、进行参数校验
        if(!array_key_exists('return_code', $data)
            ||(array_key_exists('return_code', $data) && $data['return_code'] != 'SUCCESS')) {
                //TODO失败,不是支付成功的通知
                //如果有需要可以做失败时候的一些清理处理，并且做一些监控
                $this->msg = '异常异常';
                return $this->payResult;
            }
            
            if(!array_key_exists('transaction_id', $data)){
                $this->msg = '输入参数不正确';
                return $this->payResult;
            }
            
            //TODO 2、进行签名验证
            try {
                $checkResult = $objData->CheckSign($config);
                if($checkResult == false){
                    //签名错误
                    $this->msg = '签名错误...';
                    return $this->payResult;
                }
            } catch(\Exception $e) {
                $this->msg = $e->getMessage();
                return $this->payResult;
            }
            
            
            //查询订单，判断订单真实性
            if(!$this->Queryorder($data["transaction_id"])){
                $this->msg = '订单查询失败';
                return $this->payResult;
            }
            
            
            try {
                $this->orderNo = $data['out_trade_no'];
                $this->tradeNo = $data['transaction_id'];
                $this->payResult = $this->handleResult($data, $this->msg);
                if(!$this->payResult){
                    $this->callbackData = $data;
                }
            } catch (\Exception $e) {
               $this->msg = $e->getMessage();
            }
            
            return $this->payResult;
    }
    
    
    //此方法不是微信提供的，自己写，具体业务类重写此方法
    public function handleResult ($data, &$msg){
        return $this->{$this->action}($data, $msg);
    }
    
    
    
    public function chargeNotify($data, &$msg) { //充电支付
        return PayHandle::charge($data, $msg, 0);
    }
    
    public function extendChargeNotify($data, &$msg) { //充电支付
        return PayHandle::extendCharge($data, $msg, 0);
    }

}






